import http from '../http/http.js';
import { request } from "../utils/request";
import qs from "qs";
import {
    getMD5Sign,
    getRandomText
} from "../utils/util.js";

export default {
    miniUnifiedOrder({ products, receiveAddressId, couponRecordId, tradeTypeId } = {}) {
        let data = {
            products,
            receiveAddressId,
            couponRecordId,
            tradeTypeId
        }
        data.random = getRandomText();
        data.sign = getMD5Sign(data, true);
        return http({
            method: "post",
            data: data,
            url: "/api/wechat/pay/mini/unified/order",
        }).catch((e) => {
            return e;
        });
    },
    miniUnifiedOrderV2({ products, receiveAddressId, couponRecordId } = {}) {
        let data = {
            products,
            receiveAddressId,
            couponRecordId,
        }
        return http({
            method: "post",
            data: data,
            url: "/api/pay/wechat/jsapi",
        }).catch((e) => {
            return e;
        });
    },
    miniUnifiedOrderAli({ products, receiveAddressId, couponRecordId, tradeTypeId } = {}) {
        let data = {
            products,
            receiveAddressId,
            couponRecordId,
            tradeTypeId
        }
        data.random = getRandomText();
        data.sign = getMD5Sign(data, true);
        return http({
            method: "post",
            data: data,
            url: "/api/ali/pay/unified/order",
        }).catch((e) => {
            return e;
        });
    },
    miniUnifiedOrderWechat({ products, receiveAddressId, couponRecordId, tradeTypeId } = {}) {
        let data = {
            products,
            receiveAddressId,
            couponRecordId,
            tradeTypeId
        }
        data.random = getRandomText();
        data.sign = getMD5Sign(data, true);
        return http({
            method: "post",
            data: data,
            url: "/api/wechat/pay/mini/unified/order",
        }).catch((e) => {
            return e;
        });
    },
    appUnifiedOrderWechat({ products, receiveAddressId, couponRecordId } = {}) {
        let data = {
            products,
            receiveAddressId,
            couponRecordId,
        }
        return http({
            method: "post",
            data: data,
            url: "/api/pay/wechat/app",
        }).catch((e) => {
            return e;
        });
    },
    appUnifiedOrderAli({ products, receiveAddressId, couponRecordId } = {}) {
        let data = {
            products,
            receiveAddressId,
            couponRecordId,
        }
        return http({
            method: "post",
            data: data,
            url: "/api/pay/ali/app",
        }).catch((e) => {
            return e;
        });
    },
    againOrder({ orderNo } = {}) {
        let data = {
            orderNo
        }
        data.random = getRandomText();
        data.sign = getMD5Sign(data, true);
        return http({
            method: "post",
            data: data,
            url: "/api/order/again",
        }).catch((e) => {
            return e;
        });
    },
    againOrderV2({ orderNo } = {}) {
        return http({
            method: "post",
            url: "/api/pay/again/" + orderNo,
        }).catch((e) => {
            return e;
        });
    },
    againOrderAli({ orderNo } = {}) {
        let data = {
            orderNo
        }
        data.random = getRandomText();
        data.sign = getMD5Sign(data, true);
        return http({
            method: "post",
            data: data,
            url: "/api/ali/pay/again/order",
        }).catch((e) => {
            return e;
        });
    },
    againOrderWechat({ orderNo } = {}) {
        let data = {
            orderNo
        }
        data.random = getRandomText();
        data.sign = getMD5Sign(data, true);
        return http({
            method: "post",
            data: data,
            url: "/api/wechat/pay/again/order",
        }).catch((e) => {
            return e;
        });
    },
    closeOrder({ orderNo } = {}) {
        let data = {
            orderNo
        }
        data.random = getRandomText();
        data.sign = getMD5Sign(data, true);
        return http({
            method: "post",
            data: data,
            url: "/api/order/close",
        }).catch((e) => {
            return e;
        });
    },
    orderStatusChange({ orderNo, orderStatus } = {}) {
        let data = {
            orderNo,
            orderStatus
        }
        data.random = getRandomText();
        data.sign = getMD5Sign(data, true);
        return http({
            method: "post",
            data: data,
            url: "/api/order/status",
        }).catch((e) => {
            return e;
        });
    },
	syncOrder({ orderNo } = {}) {
	    return http({
	        method: "get",
			params: {
			    orderNo
			},
			url: "/api/order/sync",
	    }).catch((e) => {
	        return e;
	    });
	},
    orderDetail({ orderNo } = {}) {
        let data = {
            orderNo,
        }
        return request({
            method: "GET",
            data: {
                orderNo
            },
            url: "/api/order/detail",
        }).catch((e) => {
            return e;
        });
    },
    getExpressStatus({ lId, orderNo, deliveryId } = {}) {
        let data = {
            lId,
            orderNo,
            deliveryId
        }
        return http({
            method: "get",
            data: data,
            url: "/api/order/logistics",
        }).catch((e) => {
            return e;
        });
    },
    orderRemind({ orderNo } = {}) {
        return http({
            method: "get",
            params: {
                orderNo
            },
            url: "/api/order/remind",
        }).catch((e) => {
            return e;
        });
    },
    evaluationSave({ data } = {}) {
        return http({
            method: "post",
            data: data,
            url: "/api/product/evaluation/save",
        }).catch((e) => {
            return e;
        });
    },
    evaluationQuery({ productId, pageNo, pageSize } = {}) {
        return http({
            method: "get",
            params: { productId, pageNo, pageSize },
            url: "/api/product/evaluation/query",
        }).catch((e) => {
            return e;
        });
    },
    refundOrderWx({ orderNo } = {}) {
        return http({
            method: "get",
            params: { orderNo },
            url: "/api/wechat/pay/refund/order",
        }).catch((e) => {
            return e;
        });
    },
    refundOrderAli({ orderNo } = {}) {
        return http({
            method: "get",
            params: { orderNo },
            url: "/api/ali/pay/refund/order",
        }).catch((e) => {
            return e;
        });
    },
    refundOrder({ orderNo } = {}) {
        return http({
            method: "get",
            params: { orderNo },
            url: "/api/order/refund",
        }).catch((e) => {
            return e;
        });
    },
    aftersaleList({ current, size, afterSaleType, afterSaleStatus, orderNo, afterSaleNo, productId } = {}) {
        const params = {
            current,
            size,
            afterSaleType,
            afterSaleStatus,
            orderNo,
            afterSaleNo,
            productId
        }
        // 如果params中任意值为undefined则删除该键值对
        Object.keys(params).forEach(key => {
            if (params[key] === undefined) {
                delete params[key];
            }
        });
        return request({
            method: "GET",
            data: params,
            url: "/api/order/aftersale"
        }).catch((e) => {
            return e;
        });
    },
    aftersaleOrder({ orderNo, afterSaleType, afterSaleGoodStatus, remark, images, questionType, productId, id } = {}) {
        return http({
            method: "post",
            data: { orderNo, afterSaleType, afterSaleGoodStatus, remark, images, questionType, productId, id },
            url: "/api/order/aftersale",
        }).catch((e) => {
            return e;
        });
    },
    aftersaleDetail({ afterSaleType, afterSaleStatus, orderNo, afterSaleNo, userId, productId } = {}) {
        return http({
            method: "get",
            data: { afterSaleType, afterSaleStatus, orderNo, afterSaleNo, userId, productId },
            url: "/api/order/aftersale/detail",
        }).catch((e) => {
            return e;
        });
    },
    cancelAftersale({ afterSaleNo } = {}) {
        return http({
            method: "post",
            url: "/api/order/aftersale/cancel/" + afterSaleNo,
        }).catch((e) => {
            return e;
        });
    },
    aftersaleLogistics({ afterSaleNo, lid } = {}) { // 售后物流信息
        return http({
            method: "post",
            data: { afterSaleNo, lid },
            url: "/api/order/aftersale/logistics",
        }).catch((e) => {
            return e;
        });
    },
    aftersaleLogisticsQuery({ afterSaleNo, lId } = {}) { // 售后物流信息
        return http({
            method: "get",
            params: { afterSaleNo, lId },
            url: "/api/order/aftersale/logistics",
        }).catch((e) => {
            return e;
        });
    },
    aftersaleConfirm({ afterSaleNo } = {}) { // 售后确认收货
        return http({
            method: "post",
            url: "/api/order/aftersale/confirm/" + afterSaleNo,
        }).catch((e) => {
            return e;
        });
    },
    editOrderAddress({ orderNo, receiverName, receiverTel, receiverProvince, receiverCity, receiverArea, receiverAddress } = {}) {
        return http({
            method: "post",
            data: { orderNo, receiverName, receiverTel, receiverProvince, receiverCity, receiverArea, receiverAddress },
            url: "/api/order/address",
        }).catch((e) => {
            return e;
        });
    },
};
